<template>
  <div class="login">
    <div class="login_list">
      <div class="header">
        <img src="../assets/img/2.jpg" alt="">
      </div>
      <div class="from">
        <el-form :model="loginForm" :rules="rules" ref="ruleform" label-width="100px">
          <el-form-item prop="name" label-width="0px">
            <el-row :gutter="20">
              <el-col :span="18" :offset="3">
                <el-input type="text" placeholder="用户名" v-model="loginForm.name"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label-width="0px" prop="password">
            <el-row :gutter="20">
              <el-col :span="18" :offset="3">
                <el-input type="password" placeholder="登录密码" v-model="loginForm.password" @keydown.enter.native = "handleSumbit('ruleform')"></el-input>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label-width="0px">
            <el-button type="primary" class="login_btn" @click="handleSumbit('ruleform')">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="reg">
        <a href="#">忘记密码</a>
      </div>
    </div>
  </div>
</template>

<script>
console.log('这是登录页面！！！ 别搞我')
export default {
  data () {
    return {
      // 登录数据
      loginForm: {
        name: '',
        password: ''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '账户不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 登录请求
    handleSumbit (val) {
      this.$refs[val].validate(async (v) => {
        if (!v) return this.$message.error('请认真填写')
        var { data: res } = await this.$http.post('/login', this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message.error('账户密码错误')
        }
        // 登录成功
        this.$message.success('登录成功')
        // 清空账户
        this.$refs[val].resetFields()
        // token保存在session里面
        var token = res.data[0].token
        // 设置token
        window.sessionStorage.setItem('token', token)
        // 编程式导航跳转到后台列表
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login{
  position: relative;
  height: 100%;
  width: 100%;
  background: url('//s1.ax1x.com/2020/05/24/Yz079g.jpg');
  background-size: cover;
  background-position: center center;
}
.login_list{
  position: absolute;
  left: 50%;
  top: 50%;
  height: 500px;
  width: 400px;
  background-color: #fff;
  transform:  translate(-50%, -50%);
  border-radius: 15px;
}
.header{
  margin: 50px 0;
  text-align: center;
  height: 100px;
  img{
    height: 100px;
    border-radius: 50%;
  }
}
.login_btn{
  margin: 50px 50px 0 50px;
  height: 40px;
  width: 300px;
  border-radius: 20px;
}
.reg{
  padding: 20px;
  a{
    font-size: 12px;
    color: #666;
  }
}
</style>
